
	
***** find wind direction associated with maximum pollution for each monitor group 

***Inputs: 
* $Data/GameUserWPdaily_norm.dta
* $Data/AQS_clusters_distance_seed.dta

***Outputs 
* $Data/group100directionsmonth_seed60.dta
* $Data/group75directionsmonth_seed60.dta
* $Data/group125directionsmonth_seed60.dta
* $Results/table_monitorgroups_seed.tex

		
	use $Data/GameUserWPdaily_norm.dta, clear
	
	gen month = month(date)
	
	gen highpm =  weighted_dailypm > 25 & weighted_dailypm<. 
	
	gen monthyear = mofd(date)

	
	egen cut_winddirection = cut(median_closestwind), at(0(60)360)
	replace cut_winddirection = 0 if median_closestwind ==360

	
	
	merge m:1 zip using $Data/AQS_clusters_distance_seed.dta
		
	forval mgroup = 1(1)125 { // max number of monitor groups 
		
		
	** 100 cluster groups 
	quietly	capture	reghdfe highpm  i.cut_wind#i.month  if monitorcluster ==`mgroup' ,   absorb( anon_id  monthyear) cluster(anon_id monthyear )
				
	capture	regsave using $Results/group100zipwind60month_seed`mgroup', replace 
		
		
	** 75 cluster group 
	
	quietly	capture reghdfe highpm  i.cut_wind#i.month  if altmonitorcluster75 ==`mgroup' ,   absorb( anon_id  monthyear) cluster(anon_id monthyear )
				
	capture		regsave using $Results/group75zipwind60month_seed`mgroup', replace 
			
	
	** 125 cluster group 
	
	quietly	capture reghdfe highpm  i.cut_wind#i.month  if altmonitorcluster125 ==`mgroup' ,   absorb( anon_id  monthyear) cluster(anon_id monthyear )
				
	capture		regsave using $Results/group125zipwind60month_seed`mgroup', replace 
		
	}
	
			
*** compile results when number of clusters is 100 	
			
		clear 
		
		gen monitorcluster =. 
		
		forval mgroup = 1(1)100 {
		
		capture append using  $Results/group100zipwind60month_seed`mgroup'
		capture erase  $Results/group100zipwind60month_seed`mgroup'
		
		replace monitorcluster = `mgroup' if monitorcluster ==. 
		
	}
	

	gen direction = substr(var,1,3)
	destring direction, replace force ignore("c" "."  "u" "ons" "b")

	gen month = substr(var,-9,3)
	destring month, replace force ignore("c" "."  "u" "o" "b" "#" "n")

	
	replace coef = . if r2==. // 
	
	by monitorcluster month, sort: egen maxcoef= max(coef) if direction<.  & coef <. 


	gen maxdirection_group1 = direction if maxcoef ==coef & maxcoef >=0  & maxcoef<. 
	

	
	collapse (firstnm) maxdirection_group1 , by(monitorcluster month)
	
	drop if month ==. 
		
	save $Data/group100directionsmonth_seed60.dta, replace
	
	

	
		
		
		
		
		
*** compile results when number of clusters is 75  	
			
		clear 
		
		gen altmonitorcluster75 =. 
		
		forval mgroup = 1(1)75 {
		
		capture append using  $Results/group75zipwind60month_seed`mgroup'
		capture erase  $Results/group75zipwind60month_seed`mgroup'
		
		replace altmonitorcluster75 = `mgroup' if altmonitorcluster75 ==. 
		
	}
	

	gen direction = substr(var,1,3)
	destring direction, replace force ignore("c" "."  "u" "ons" "b")

	gen month = substr(var,-9,3)
	destring month, replace force ignore("c" "."  "u" "o" "b" "#" "n")

	
	replace coef = . if r2==. // 
	
	by altmonitorcluster75 month, sort: egen maxcoef= max(coef) if direction<.  & coef <. 


	gen maxdirection_group2 = direction if maxcoef ==coef & maxcoef >=0  & maxcoef<. 
	

	
	collapse (firstnm) maxdirection_group2 , by(altmonitorcluster75 month)
	
	drop if month ==. 
		
	save $Data/group75directionsmonth_seed60.dta, replace
	
	

		
		
*** compile results when number of clusters is 125	
			
		clear 
		
		gen altmonitorcluster125 =. 
		
		forval mgroup = 1(1)125 {
		
		capture append using  $Results/group125zipwind60month_seed`mgroup'
		capture erase  $Results/group125zipwind60month_seed`mgroup'
		
		replace altmonitorcluster125 = `mgroup' if altmonitorcluster125 ==. 
		
	}
	

	gen direction = substr(var,1,3)
	destring direction, replace force ignore("c" "."  "u" "ons" "b")

	gen month = substr(var,-9,3)
	destring month, replace force ignore("c" "."  "u" "o" "b" "#" "n")

	
	replace coef = . if r2==. // 
	
	by altmonitorcluster125 month, sort: egen maxcoef= max(coef) if direction<.  & coef <. 


	gen maxdirection_group3 = direction if maxcoef ==coef & maxcoef >=0  & maxcoef<. 
	

	
	collapse (firstnm) maxdirection_group3 , by(altmonitorcluster125 month)
	
	drop if month ==. 
		
	save $Data/group125directionsmonth_seed60.dta, replace
		
		
		
		
		
		
		
		
		
*** use instruments to estimate main results 
		
		
	use $Data/GameUserWPdaily_norm.dta, clear
		
		
		
	merge m:1 zip using $Data/AQS_clusters_distance_seed.dta
	drop _merge 
	
	gen month =month(date)
	
	merge m:1 monitorcluster month using $Data/group100directionsmonth_seed60.dta
	
	keep if _merge ==3
	drop _merge 
	
	
	
	merge m:1 altmonitorcluster75 month using $Data/group75directionsmonth_seed60.dta
	
	keep if _merge ==3
	drop _merge 
	
	
	merge m:1 altmonitorcluster125 month using $Data/group125directionsmonth_seed60.dta
	
	keep if _merge ==3
	drop _merge 
	
	
	gen monthyear = mofd(date)
	
	gen highpm =  weighted_dailypm > 25 & weighted_dailypm<. 

label variable highpm "Daily PM2.5 $>$ 25" 

	egen cut_winddirection = cut(median_closestwind), at(0(60)360)
	replace cut_winddirection = 0 if median_closestwind ==360
	
	gen maxzipwind_group1 = (cut_winddirection ==maxdirection_group1 )
	replace maxzipwind_group1 = . if maxdirection_group1==.
	
	
	gen maxzipwind_group2 = (cut_winddirection ==maxdirection_group2 )
	replace maxzipwind_group2 = . if maxdirection_group2==.
	
	
	gen maxzipwind_group3 = (cut_winddirection ==maxdirection_group3 )
	replace maxzipwind_group3 = . if maxdirection_group3==.
	
	
	egen cut_temp = cut(weighted_temperature), at(-40(10)40)
		
		
		
ivreghdfe norm_score (highpm =maxzipwind_group1 ) weighted_wind_speed weighted_precipitation , absorb(anon_id monthyear cut_temp) cluster(anon_id monthyear)

eststo mgroup1



		
ivreghdfe norm_score (highpm =maxzipwind_group2 )  weighted_wind_speed weighted_precipitation, absorb(anon_id monthyear cut_temp ) cluster(anon_id monthyear)

eststo mgroup2




ivreghdfe norm_score (highpm =maxzipwind_group3 ) weighted_wind_speed weighted_precipitation , absorb(anon_id monthyear cut_temp) cluster(anon_id monthyear)

eststo mgroup3

label variable highpm "Daily PM2.5 $>$ 25" 

	esttab mgroup2 mgroup1 mgroup3 using $Results/table_monitorgroups_seed.tex, keep(highpm) replace se scalars("widstat First stage F") nocon star(* 0.1 ** 0.05 *** 0.001) label nomtitles
	
